package cn.mw.cmdb.processor;

import cn.mw.cmdb.entity.InstanceInfo;
import cn.mw.microMonitorCommon.util.EnDecryptionUtils;
import cn.mw.microMonitorCommon.util.RSAUtils;
import org.springframework.util.StringUtils;

import java.util.List;

import static cn.mw.cmdb.enums.ZabbixAssetsStatusEnum.SHUTDOWN;
import static cn.mw.cmdb.enums.ZabbixAssetsStatusEnum.UNKNOWN;
import static cn.mw.cmdb.util.AssetCodeGenerator.generateAssetCode;
import static cn.mw.cmdb.util.ModelUtils.checkStrIsNumber;
import static cn.mw.cmdb.util.ValConvertUtil.*;

public interface MonitorValue extends PropertyValueManage {
    public int encryptDataLength = 172;
    public String encryptInfoMatch = "\\*+";
    public static final String MonitorServerIdKey = Prefix + "monitorServerId";
    public static final String HostNameKey = Prefix + "hostName";
    public static final String HostGroupIdKey = Prefix + "hostGroupId";
    public static final String InBandIpKey = Prefix + "inBandIp";

    public static final String InStanceNameKey = Prefix + "instanceName";

    public static final String RuleTypeKey = Prefix + "ruleType";
    public static final String TrunkMonitorFlagKey = Prefix + "trunkMonitorFlag";

    public static final String EngineSourceKey = Prefix + "engineSource";
    public static final String EngineIdKey = "engineId";

    public static final String PortKey = Prefix + "port";

    public static final String CommunityKey = Prefix + "community";

    public static final String SecurityNameKey = Prefix + "securityName";

    public static final String SecurityLevelKey = Prefix + "securityLevel";

    public static final String ContextNameKey = Prefix + "contextName";
    //验证协议、验证算法
    public static final String AuthAlgNameKey = Prefix + "authAlgName";
    //验证口令
    public static final String AuthValueKey = Prefix + "authValue";
    //隐私协议、加密算法
    public static final String PrivAlgNameKey = Prefix + "privAlgName";

    public static final String PriValueKey = Prefix + "priValue";

    public static final String AccountKey = Prefix + "account";

    public static final String PasswordKey = Prefix + "password";

    public static final String TemplateIdKey = Prefix + "templateId";
    public static final String TemplateNameKey = Prefix + "templateName";

    public static final String HostTemplateIdKey = Prefix + "hostTemplateId";

    public static final String ZabbixStatusKey = Prefix + "monitorFlag";

    public static final String CheckNowFlagKey = Prefix + "checkNowFlag";

    public static final String MacrosKey = Prefix + "macros";

    public static final String PollingEngineKey = Prefix + "pollingEngine";

    public static final String MonitorTypeKey = Prefix + "monitorType";

    public static final String MonitorTypeIdKey = Prefix + "monitorTypeId";

    public static final String MonitorHttpTestId = Prefix + "httpTestId";

    public static final String MonitorTypeNameKey = Prefix + "monitorTypeName";

    public static final String SpecificationKey = Prefix + "specification";

    public static final String BrandKey = Prefix + "brand";
    public static final String InterfacesTypeKey = Prefix + "interfacesType";

    public static final String MonitorModeKey = Prefix + "monitorMode";

    public static final String ItemAssetsStatusKey = Prefix + "itemAssetsStatus";
    public static final String AssetsCodeKey = Prefix + "asset_coding";
    public static final String FacilityDescriptorKey = "facilityDescriptor";
    public static final String FinanceAssetCodeKey = "assetCode";

    public static final String FunctionModule = "functionModule";

    public static final String ProxyTypeKey = Prefix + "proxyType";

    public static final String UManageTipMessageKey = "UManageTipMessage";

    public static String BatchIndex = "batchIndex";
    public static String modelIdKey = "modelId";
    public static String RandomNameKey = "randomNameFlag";

    public static final String DescKey = Prefix + "desc";
    //默认宏值，方便告警管理通过实例id关联资产信息
    public static final String defaultMarcos = "{$MW.INSTANCE.ID}";

    default Integer getMonitorServerId() {
        Integer monitorServerId = intValueConvert(getValue(MonitorServerIdKey));
        return monitorServerId;
    }

    default Integer getProxyType() {
        Object value = getValue(ProxyTypeKey);
        if(value instanceof String){
            if(checkStrIsNumber((String)value)){
                return Integer.valueOf((String) value);
            }else{
                return null;
            }
        }
        if(value instanceof Integer){
            return (Integer) getValue(ProxyTypeKey);
        }
        return null;
    }

    default void setProxyType(Integer monitorProxyType) {
        setValue(ProxyTypeKey, monitorProxyType);
    }

    default Boolean getTrunkMonitorFlagKey() {
        Boolean trunkMonitorFlag = booleanValueConvert(getValue(TrunkMonitorFlagKey));
        return trunkMonitorFlag;
    }

    default void setTrunkMonitorFlagKey(Boolean trunkMonitorFlag) {
        setValue(TrunkMonitorFlagKey, trunkMonitorFlag);
    }


    default String getHostName() {
        String value = (String) getValue(HostNameKey);
        return value;
    }

    default List<String> getFunctionModule() {
        List<String> functionModule = (List<String>) getValue(FunctionModule);
        return functionModule;
    }

    default Object getHostGroupId() {
        return getValue(HostGroupIdKey);
    }

    default void setHostGroupId(Integer hostGroupId) {
        setValue(HostGroupIdKey, hostGroupId);
    }

    default String getUManageTipMessage() {
        return strValueConvert(getValue(UManageTipMessageKey));
    }

    default void setUManageTipMessage(String UManageTipMessage) {
        setValue(UManageTipMessageKey, UManageTipMessage);
    }


    default void setItemAssetsStatus(String assetsStatus) {
        setValue(ItemAssetsStatusKey, assetsStatus);
    }

    default void setAssetsCodeKey(String assetsCode) {
        setValue(AssetsCodeKey, assetsCode);
    }

    default String getAssetsCodeKey() {
        String value = (String) getValue(AssetsCodeKey);
        return value;
    }

    default void setRandomNameKey(Boolean randomNameKey) {
        setValue(RandomNameKey, randomNameKey);
    }

    default boolean getRandomNameKey() {
        boolean value = booleanValueConvert(getValue(RandomNameKey));
        return value;
    }

    default void setFacilityDescriptorKey(String facilityDescriptor) {
        setValue(FacilityDescriptorKey, facilityDescriptor);
    }

    default String getFacilityDescriptorKey() {
        String value = (String) getValue(FacilityDescriptorKey);
        return value;
    }

    default void setFinanceAssetCodeKey(String FinanceAssetCode) {
        setValue(FinanceAssetCodeKey, FinanceAssetCode);
    }

    default String getFinanceAssetCodeKey() {
        String value = (String) getValue(FinanceAssetCodeKey);
        return value;
    }

    default String getItemAssetsStatus() {
        String value = (String) getValue(ItemAssetsStatusKey);
        return value;
    }

    default String getInBandIp() {
        String value = (String) getValue(InBandIpKey);
        return value;
    }


    default void setBatchIndex(Integer batchIndex) {
        setValue(BatchIndex, batchIndex);
    }

    default Integer getBatchIndex() {
        Integer value = intValueConvert(getValue(BatchIndex));
        return value;
    }

    default void setEngineSourceKey(Integer engineSource) {
        setValue(EngineSourceKey, engineSource);
    }

    default Integer getEngineSourceKey() {
        if (getValue(EngineSourceKey) == null || intValueConvert(getValue(EngineSourceKey)) == 0) {
            return null;
        }
        Integer value = intValueConvert(getValue(EngineSourceKey));
        return value;
    }


    default Integer getEngineIdeKey() {
        if (getValue(EngineIdKey) == null || intValueConvert(getValue(EngineIdKey)) == 0) {
            return null;
        }
        Integer value = intValueConvert(getValue(EngineIdKey));
        return value;
    }

    default String getRuleType() {
        String value = (String) getValue(RuleTypeKey);
        return value;
    }

    default Integer getPort() {
        Integer value = intValueConvert(getValue(PortKey));
        return value;
    }

    default void setPort(int port) {
        setValue(PortKey, port);
    }

    default String getCommunity() {
        String value = (String) getValue(CommunityKey);
        return value;
    }

    default void setCommunity(String community) {
        setValue(CommunityKey, community);
    }

    default String getSecurityName() {
        String value = (String) getValue(SecurityNameKey);
        return value;
    }

    default String getSecurityLevel() {
        String value = (String) getValue(SecurityLevelKey);
        return value;
    }

    default String getContextName() {
        String value = (String) getValue(ContextNameKey);
        return value;
    }

    default String getAuthAlgName() {
        String value = (String) getValue(AuthAlgNameKey);
        return value;
    }

    default String getAuthValue() {
        String value = (String) getValue(AuthValueKey);
        return value;
    }

    default void setAuthValue(String authValue) {
        setValue(AuthValueKey, authValue);
    }

    default String getPrivAlgName() {
        String value = (String) getValue(PrivAlgNameKey);
        return value;
    }

    default String getPriValue() {
        String value = (String) getValue(PriValueKey);
        return value;
    }

    default void setPriValue(String value) {
        setValue(PriValueKey, value);
    }

    default String getAccount() {
        String value = (String) getValue(AccountKey);
        return value;
    }

    default String getPassword() {
        String value = (String) getValue(PasswordKey);
        return value;
    }

    default String getTemplateId() {
        String value = (String) getValue(TemplateIdKey);
        return value;
    }

    default void setTemplateId(String templateId) {
        setValue(TemplateIdKey, templateId);
    }

    default String getTemplateName() {
        String value = (String) getValue(TemplateNameKey);
        return value;
    }


    default String getHostTemplateId() {
        String value = (String) getValue(HostTemplateIdKey);
        return value;
    }

    default void setHostTemplateId(String hostTemplateId) {
        setValue(HostTemplateIdKey, hostTemplateId);
    }

    default Integer getZabbixStatus() {
        String value = (String) getValue(ItemAssetsStatusKey);
        if (UNKNOWN.getName().equals(value) || SHUTDOWN.getName().equals(value)) {
            return 0;
        }
        int ret = 0;
        Boolean status = booleanValueConvert(getValue(ZabbixStatusKey));
        if (null != status) {
            ret = status ? 0 : 1;
        }
        return ret;
    }

    default Boolean getMonitorStatus() {
        String value = (String) getValue(ItemAssetsStatusKey);
        if (UNKNOWN.getName().equals(value) || SHUTDOWN.getName().equals(value)) {
            return false;
        }
        Boolean status = (null == getZabbixStatus() ? false : booleanValueConvert(getValue(ZabbixStatusKey)));
        return status;
    }

    default void setZabbixStatus(boolean status) {
        setValue(ZabbixStatusKey, status);
    }

    default Boolean getCheckNowFlag() {
        Boolean ret = (Boolean) getValue(CheckNowFlagKey);
        return ret;
    }

    default List<Macros> getMacrosList() {
        List value = (List) getValue(MacrosKey);
        return value;
    }

    default String getPollingEngine() {
        String value = (String) getValue(PollingEngineKey);
        return value;
    }

    default void setPollingEngine(String value) {
        setValue(PollingEngineKey, value);
    }

    default String getMonitorType() {
        String value = (String) getValue(MonitorTypeKey);
        return value;
    }

    default Integer getMonitorMode() {
        Integer value = intValueConvert(getValue(MonitorModeKey));
        return value;
    }

    default Integer getInterfacesType() {
        Integer value = intValueConvert(getValue(InterfacesTypeKey));
        return value;
    }

    default void setInterfacesType(Integer type) {
        setValue(InterfacesTypeKey, type);
    }


    default void setMonitorType(String type) {
        setValue(MonitorTypeKey, type);
    }

    default String getMonitorTypeId() {
        String value = (String) getValue(MonitorTypeIdKey);
        return value;
    }

    default String getHttpTestId() {
        String value = (String) getValue(MonitorHttpTestId);
        return value;
    }

    default void setMonitorTypeId(String typeId) {
        setValue(MonitorTypeIdKey, typeId);
    }

    default String getMonitorTypeName() {
        String value = (String) getValue(MonitorTypeNameKey);
        return value;
    }

    default void setMonitorTypeName(String typeName) {
        setValue(MonitorTypeNameKey, typeName);
    }

    default String getSpecification() {
        String value = (String) getValue(SpecificationKey);
        return value;
    }

    default String getBrand() {
        String value = (String) getValue(BrandKey);
        return value;
    }


    default String getDesc() {
        String value = (String) getValue(DescKey);
        return value;
    }

    default String getInstanceName() {
        String value = (String) getValue(InStanceNameKey);
        return value;
    }

    String getId();

    default void setEncrypt(boolean sign) {
        return;
    }

    default boolean getEncrypt() {
        return false;
    }

    /**
     * 加密
     */
    default void encryptInfo() {
        setEncrypt(true);
        encAndDeInfo();
    }


    /**
     * 解密
     */
    default void decryptInfo() {
        setEncrypt(false);
        encAndDeInfo();
    }

    default void encAndDeInfo() {
        String community = getCommunity();
        if (StringUtils.hasText(community) && !community.matches(encryptInfoMatch)) {
            String encCommunity = EnDecryptionUtils.entrypt(community);
            setCommunity(encCommunity);
        } else {
            removeField(CommunityKey);
        }
        String value = getAuthValue();
        if (StringUtils.hasText(value) && !value.matches(encryptInfoMatch)) {
            String encValue = EnDecryptionUtils.entrypt(value);
            setAuthValue(encValue);
        } else {
            removeField(AuthValueKey);
        }
        value = getPriValue();
        if (StringUtils.hasText(value) && !value.matches(encryptInfoMatch)) {
            String encValue = EnDecryptionUtils.entrypt(value);
            setPriValue(encValue);
        } else {
            removeField(PriValueKey);
        }
    }

    /**
     * 加密（公钥加密）
     */
    default void encryptInfoHandle() {
        String community = getCommunity();
        if (StringUtils.hasText(community) && community.length() != encryptDataLength) {
            String encCommunity = RSAUtils.encryptData(community, RSAUtils.RSA_PUBLIC_KEY);
            setCommunity(encCommunity);
        }
        String value = getAuthValue();
        if (StringUtils.hasText(value) && value.length() != encryptDataLength) {
            String encValue = RSAUtils.encryptData(value, RSAUtils.RSA_PUBLIC_KEY);
            setAuthValue(encValue);
        }

        String priValue = getPriValue();
        if (StringUtils.hasText(priValue) && priValue.length() != encryptDataLength) {
            String encPriValue = RSAUtils.encryptData(priValue, RSAUtils.RSA_PUBLIC_KEY);
            setPriValue(encPriValue);
        }
    }

    /**
     * 解密（秘钥解密）
     */
    default void decryptInfoHandle() {
        String community = getCommunity();
        if (StringUtils.hasText(community) && community.length() == encryptDataLength && community.endsWith("=")) {
            String decryptCommunity = RSAUtils.decryptData(community, RSAUtils.RSA_PRIVATE_KEY);
            setCommunity(decryptCommunity);
        }
        String value = getAuthValue();
        if (StringUtils.hasText(value) && value.length() == encryptDataLength && value.endsWith("=")) {
            String decryptValue = RSAUtils.decryptData(value, RSAUtils.RSA_PRIVATE_KEY);
            setAuthValue(decryptValue);
        }
        String priValue = getPriValue();
        if (StringUtils.hasText(priValue) && priValue.length() == encryptDataLength && priValue.endsWith("=")) {
            String decryptPriValue = RSAUtils.decryptData(priValue, RSAUtils.RSA_PRIVATE_KEY);
            setPriValue(decryptPriValue);
        }
    }


    default void editorAssetCode(InstanceInfo instanceInfo) throws Exception {
        if (instanceInfo.isDeviceCodeFlag()) {
            //获取
            String value = generateAssetCode(instanceInfo.getModelId());
            setAssetsCodeKey(value);
        }
    }


}
